package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.entity.Event;
import cn.zucc.edu.handyzucc.entity.EventReg;
import cn.zucc.edu.handyzucc.entity.EventRegExtend;
import cn.zucc.edu.handyzucc.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/event")
public class EventController {
    @Autowired
    private EventService eventService;

    @RequestMapping(value = "getAllEvents", method = RequestMethod.GET)
    private Map<String, Object> getAllEvents() {
        Map<String, Object> modelMap = new HashMap<>();
        List<Event> events = eventService.getAllEvents();
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", events);
        return modelMap;
    }

    @RequestMapping(value = "getEventDetailById", method = RequestMethod.POST)
    private Map<String, Object> getEventDetailById(Integer event_id) {
        Map<String, Object> modelMap = new HashMap<>();
        Event event = eventService.getEventDetailById(event_id);
        modelMap.put("code", "0");
        modelMap.put("info", "查询成功");
        modelMap.put("data", event);
        return modelMap;
    }

    @RequestMapping(value = "eventReg", method = RequestMethod.POST)
    private Map<String, Object> eventReg(Integer eventreg_event, Integer eventreg_user) {
        Map<String, Object> modelMap = new HashMap<>();
        if (eventService.eventReg(eventreg_event, eventreg_user)) {
            modelMap.put("code", "0");
            modelMap.put("info", "报名成功");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "报名失败");
        }
        return modelMap;
    }

    @RequestMapping(value = "eventAlreadyReg", method = RequestMethod.POST)
    private Map<String, Object> eventAlreadyReg(Integer eventreg_event, Integer eventreg_user) {
        Map<String, Object> modelMap = new HashMap<>();
        if (eventService.eventAlreadyReg(eventreg_event, eventreg_user)) {
            modelMap.put("code", "0");
            modelMap.put("info", "已经报名");
        } else {
            modelMap.put("code", "-1");
            modelMap.put("info", "还未报名");
        }
        return modelMap;
    }

    @RequestMapping(value = "addEvent", method = RequestMethod.POST)
    private Map<String, Object> addEvent(@RequestBody Map<String, Object> map) throws ParseException {
        Map<String, Object> modelMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Event event = new Event(map.get("event_title").toString(), map.get("event_content").toString(), map.get("event_content_md").toString(),map.get("event_publisher").toString(), sdf.parse(map.get("event_time").toString()));
        if (eventService.addEvent(event))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "deleteEvent", method = RequestMethod.POST)
    private Map<String, Object> deleteEvent(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        if (eventService.deleteEvent(Integer.parseInt(map.get("event_id").toString())))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "updateEvent", method = RequestMethod.POST)
    private Map<String, Object> updateEvent(@RequestBody Map<String, Object> map) {
        Map<String, Object> modelMap = new HashMap<>();
        Event event = new Event(Integer.parseInt(map.get("event_id").toString()), map.get("event_title").toString(), map.get("event_content").toString(),map.get("event_content_md").toString(), map.get("event_publisher").toString(),Integer.parseInt(map.get("event_status").toString()));
        if (eventService.updateEvent(event))
            modelMap.put("code", "0");
        else modelMap.put("code", "-1");
        return modelMap;
    }

    @RequestMapping(value = "getEventReg", method = RequestMethod.POST)
    private Map<String, Object> getEventReg(@RequestBody Map<String, Object> map){
        Map<String, Object> modelMap = new HashMap<>();
        List<EventRegExtend> eventRegs = eventService.getEventReg(Integer.parseInt(map.get("eventreg_event").toString()));
        modelMap.put("code","0");
        modelMap.put("data", eventRegs);
        return modelMap;
    }
}
